package com.example.activitytest

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.activitytest.databinding.ActivityMainBinding
import com.example.activitytest.databinding.ActivitySecondBinding

class SecondActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySecondBinding//由于build.gradle中的配置，布局文件最后加了Binding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySecondBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val extraString = intent.getStringExtra("chuandi1")
        val extraData = intent.getIntExtra("chuandi2",0)//需要两个参数，除键之外，还需要一个int型的默认值
        Log.d("Second Page","$extraString")
        Log.d("Second Page int","$extraData")
        with(binding){
            tvHalo.setOnClickListener{
                val intent = Intent(this@SecondActivity,ThirdActivity::class.java)
                startActivityForResult(intent,223)//点击页面2中的textview，跳转到第3个页面
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
//        Log.d("3回传到2","回传回来的值是$resultCode,$requestCode,$data")
        when(requestCode){
            223 -> if (resultCode == RESULT_OK){
                val returnedData = data?.getStringExtra("回传1")
                Log.d("3回传到2","回传回来的值是$returnedData")
            }
        }
    }
}